#include "Utils.h"
unsigned long long seed = 1;

float drand48(void)
{
	seed = (drand48a * seed + drand48c) & 0xFFFFFFFFFFFFLL;
	unsigned int x = seed >> 16;
	return  ((float)x / (float)drand48m);

}
void srand48(unsigned int i)
{
	seed = (((long long int)i) << 16) | rand();
}

QColor vec2color(vec value)
{
	return QColor((value.x() > 1 ? 1 : value.x()) * 255, (value.y() > 1 ? 1 : value.y()) * 255, (value.z() > 1 ? 1 : value.z()) * 255);
}

vec color2vec(QColor value)
{
	return vec(1.*value.red() / 255, 1.*value.green() / 255, 1.*value.blue() / 255);
}
float clamp(const float& f0, const float& omin, const float& omax) {
	float ff = f0;

	if (ff > omax) ff = omax;
	if (ff < omin) ff = omin;

	return ff;
}
float clamp01(const float& f) {
	return clamp(f, 0, 1);
}

vec randvec()
{
	//vec v;
	//do
	//{
	//	v = vec(drand48(), drand48(), drand48());
	//	v = v * 2 - vec(1,1,1);
	//} while (v.length() > 1);
	//return v;
	
	vec rv;
	float u = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);;
	float v = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);;
	float theta = 2 * PI * u;
	float phi = acosf(2 * v - 1);
	rv = vec(sin(theta)*sin(phi),cos(theta)*sin(phi),cos(phi));
	return rv.normalized();
}

vec max(const vec& v1, const vec& v2)
{
	float x = (v1.x() > v2.x()) ? v1.x() : v2.x();
	float y = (v1.y() > v2.y()) ? v1.y() : v2.y();
	float z = (v1.z() > v2.z()) ? v1.z() : v2.z();
	return vec(x, y, z);
}


